package com.mall.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.mall.common.utils.Response;
import com.mall.common.utils.ResponseMessage;
import com.mall.entity.Attr;
import com.mall.entity.AttrGroup;
import com.mall.entity.Brand;
import com.mall.service.AttrGroupService;
import com.mall.service.AttrService;
import com.mall.vo.AttrGroupVo;
import com.mall.vo.AttrGroupWithAttrsVo;
import com.mall.vo.AttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//import com.mall.product..AttrGroup;
//import com.mall.product.service.AttrGroupService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;



/**
 * 属性分组
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-08-30 18:03:59
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;


    @Autowired
    private AttrService attrService;


    /**
     * 查询参数页
     * 请求 要spu_id
     */

    @GetMapping("/listGroupAndAttr/{spuId}")
    public Response listGroupAndAttr(@PathVariable("spuId") Long spuId){
        List<AttrGroupVo> list=attrService.selectListGroupAttr(spuId);

        return new Response(ResponseMessage.SUCCESS).data(list);
    }





    ///product/attrgroup/{catelogId}/withattr
    /**
     *获取分类下所有分组关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }


    @GetMapping("/allGroup")
    public Response listAllGroup(){
        List<AttrGroup> list = attrGroupService.list();
        return new Response(ResponseMessage.SUCCESS).data(list);
    }


    @GetMapping("/groupByid/{attrGroupId}")
    public Response groupById(@PathVariable("attrGroupId") Long id){
        List<AttrGroup> list = attrGroupService.list(new QueryWrapper<AttrGroup>().eq("attr_group_id", id));
        return new Response(ResponseMessage.SUCCESS).data(list);
    }


    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<Attr> entities =  attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
//    @GetMapping("/{attrgroupId}/noattr/relation")
//    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
//                            @RequestParam Map<String, Object> params){
//        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
//        return R.ok().put("page",page);
//    }







    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
//    @RequiresPermissions("product:attrgroup:list")
    public Response list(@PathVariable("catelogId") Long catelogId,@RequestBody Map<String, String> map){
        IPage<AttrGroup> page = attrGroupService.queryPage(catelogId,map);
        return new Response(ResponseMessage.SUCCESS).data(page);
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
//    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroup attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroup attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroup attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

//    @RequestMapping("/delete/{id}")
//    public Response delete(@PathVariable Long id){
//        boolean b = attrGroupService.removeById(id);
//        if (b == false){
//            return new Response(ResponseMessage.FAIL);
//        }
//        return new Response(ResponseMessage.SUCCESS);
//    }



    @PostMapping("/add/{catId}/{groupName}")
    public R add(@PathVariable("groupName") String groupName,@PathVariable("catId") Long catId,@RequestBody AttrsVo[] attrsVos){
        attrGroupService.saveAllAttr(groupName,catId,attrsVos);
        return new R();
    }


}
